<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { ElButton } from 'element-plus'
import { Search } from '@/components/Search'
import { useI18n } from '@/hooks/web/useI18n'
import { Table } from '@/components/Table'
import { getOperationLogListApi,getDelOperationLogApi } from '@/api/product'
import { OperationLogList } from '@/api/product/types'
import { TableColumn } from '@/types/table'
import { useTable } from '@/views/Product/useTable'
import { FormSchema } from '@/types/form'
import { useValidator } from '@/hooks/web/useValidator'
import { ref, reactive } from 'vue'
import {useEmitt} from "@/hooks/web/useEmitt";

const { t } = useI18n()

const { required } = useValidator()

const schema = reactive<FormSchema[]>([
  {
    field: 'trueName',
    label: '管理员名称',
    component: 'Input'
  },
  {
    field: 'tablename',
    label: '操作的列表',
    component: 'Input'
  },
  {
    field: 'type',
    label: '操作类型',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '添加',
          value: '2'
        },
        {
          label: '刪除',
          value: '3'
        },
        {
          label: '修改',
          value: '4'
        },
        {
          label: '查找',
          value: '5'
        }
      ]
    }
  },
  {
    field: 'createDate',
    label: '操作时间',
    component: 'DatePicker'
  }
])

const columns = reactive<TableColumn[]>([
  {
    field: 'id',
    label: '序号'
  },
  {
    field: 'trueName',
    label: '管理员名称'
  },
  {
    field: 'tablename',
    label: '操作的列表'
  },
  {
    field: 'createDate',
    label: '执行时间'
  },
  {
    field: 'type',
    label: '操作类型',
    formatter: (_: Recordable, __: TableColumn, cellValue: number) => {
      if(cellValue == 2){
        return "添加"
      }else if(cellValue == 3){
        return "删除"
      }else if(cellValue == 4){
        return "修改"
      }else if(cellValue == 5){
        return "查找"
      }
    }
  },
  {
    field: 'remark',
    label: '备注'
  },
  {
    field: 'action',
    label: t('tableDemo.action')
  }
])

//获取日誌数据
const { register, tableObject, methods } = useTable<OperationLogList>({
  getListApi: getOperationLogListApi,
  delListApi: getDelOperationLogApi,
  response: {
    list: 'list',
    total: 'total'
  },
  props: {
    columns
  }
})

useEmitt({
  name: 'getList',
  callback: (type: string) => {
    if (type === 'add') {
      tableObject.currentPage = 1
    }
    getList()
  }
})

const { getList, setSearchParams } = methods

getList()

const delLoading = ref(false)

const delData = async (row: OperationLogList | null, multiple: boolean) => {
  tableObject.currentRow = row
  const { delList, getSelections } = methods
  const selections = await getSelections()
  delLoading.value = true
  await delList(
    multiple ? selections.map((v) => v.id) : [tableObject.currentRow?.id as number],
    multiple
  ).finally(() => {
    delLoading.value = false
  })
}
</script>

<template>
  <ContentWrap :title="t('searchDemo.search')">
    <Search :schema="schema" @search="setSearchParams" @reset="setSearchParams" />
  </ContentWrap>
  <ContentWrap :title="t('productTable.operationLog')">
    <Table
      :border="true"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :data="
        tableObject.tableList.slice(
          (tableObject.currentPage - 1) * tableObject.pageSize,
          tableObject.currentPage * tableObject.pageSize
        )
      "
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      @register="register"
    >
      <template #action="{ row }">
        <ElButton type="danger" @click="delData(row, true)">
          {{ t('exampleDemo.del') }}
        </ElButton>
      </template>
    </Table>
  </ContentWrap>
</template>
